package com.xiaoyu.studyworkspace.controller;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.xiaoyu.studyworkspace.common.kapaentity.websocket.KapaCode;
import com.xiaoyu.studyworkspace.common.kapaentity.register.appbooting.AppBooting;
import com.xiaoyu.studyworkspace.common.kapaentity.register.selectinfo.SelectInfo;
import com.xiaoyu.studyworkspace.common.util.BaseContext;
import com.xiaoyu.studyworkspace.common.util.KapaHttpUtil;
import com.xiaoyu.studyworkspace.common.util.KapaParam;
import com.xiaoyu.studyworkspace.common.util.Result;
import com.xiaoyu.studyworkspace.dto.EMaCheckDTO;
import com.xiaoyu.studyworkspace.dto.EMaRegisterDTO;
import com.xiaoyu.studyworkspace.dto.EMaUserInfoDTO;
import com.xiaoyu.studyworkspace.dto.TransferDTO;
import com.xiaoyu.studyworkspace.pojo.AutoRegister;
import com.xiaoyu.studyworkspace.pojo.EMaUerInfo;
import com.xiaoyu.studyworkspace.service.AutoRegisterService;
import com.xiaoyu.studyworkspace.vo.EMaUerInfoVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/autoregister")
@Slf4j
@CrossOrigin("*")
public class AutoRegisterController {

    @Autowired
    private AutoRegisterService autoRegisterService;


    @Autowired
    private HttpServletRequest httpServletRequest;


    @GetMapping("/scan")
    public Result startScanPhone() throws IOException, InterruptedException {
        String realPath = httpServletRequest.getServletContext().getRealPath("/");
        autoRegisterService.scanPhone(realPath);
        autoRegisterService.scanPhone2(realPath);
        return Result.success(null, "开启成功！");
    }

    @PostMapping("/appear")
    public Result startAppear(@RequestBody EMaCheckDTO eMaCheckDTO) throws IOException, InvocationTargetException, NoSuchMethodException, IllegalAccessException {
        String msg = autoRegisterService.loginReq(eMaCheckDTO);
        if (msg.contains("用户不存在") || msg.contains("密码错误")) {
            Result<String> error = Result.error(msg);
            return error;
        }
        autoRegisterService.startAppear(eMaCheckDTO.getUser_id());
        Result<String> success = Result.success(null, "已经提交处理,请勿重复提交");
        return success;
    }


    @GetMapping("/registers")
    public Result getAutoRegister(EMaCheckDTO eMaCheckDTO) {
        String msg = autoRegisterService.loginReq(eMaCheckDTO);
        if (msg.contains("用户不存在") || msg.contains("密码错误")) {
            Result<String> error = Result.error(msg);
            return error;
        }
        List<AutoRegister> autoRegisters = autoRegisterService.getRegisterRecord(eMaCheckDTO.getUser_id());
        //查询用户所有的号
        Result<List<AutoRegister>> success = Result.success(autoRegisters, "查询成功！");
        return success;
    }


    @GetMapping("/test")
    public AppBooting test() throws IOException {
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put(KapaParam.DEPLOY_APP_ID, KapaCode.DEPLOY_APP_ID);
        String result = HttpUtil.post(KapaCode.URL_APPBOOTING, KapaHttpUtil.convertParam(paramMap));
        AppBooting appBooting = JSONUtil.toBean(result, AppBooting.class);
        return appBooting;
    }

    /**
     * 转账
     *
     * @return
     */
    @PostMapping("/transfer")
    public Result transfer(@RequestBody TransferDTO transferDTO) throws IOException, InterruptedException {
        EMaCheckDTO eMaCheckDTO = new EMaCheckDTO();
        eMaCheckDTO.setUser_id(Integer.parseInt(transferDTO.getUser_id()));
        eMaCheckDTO.setPassword(Integer.parseInt(transferDTO.getPassword()));
        String loginReq = autoRegisterService.loginReq(eMaCheckDTO);
        if (loginReq.contains("用户不存在") || loginReq.contains("密码错误")) {
            Result<String> error = Result.error(loginReq);
            return error;
        }
        Result<String> success = Result.success(null, "已经提交处理,请勿重复提交");
        autoRegisterService.transfer(transferDTO);
        return success;
    }


    @GetMapping("/userinfo")
    public Result getUserInfo(@RequestParam("user_id") String user_id) throws IOException {
        SelectInfo selectInfo = autoRegisterService.getUserInfo(user_id);
        if (selectInfo == null) {
            Result<String> error = Result.error("查询失败");
            return error;
        }
        return Result.success(selectInfo, "查询成功");
    }


//    @PostMapping("/upload")
//    public Result upLoadFile(@RequestPart("file") MultipartFile multipartFile) throws IOException, InterruptedException {
//        String currentId = BaseContext.getCurrentId();
//        String uuid = UUID.randomUUID().toString();
//        int lastIndexOf = multipartFile.getOriginalFilename().lastIndexOf(".");
//        String lastName = multipartFile.getOriginalFilename().substring(lastIndexOf);
//        String fileName = uuid + lastName;
//        String realPath = httpServletRequest.getServletContext().getRealPath("/") + fileName;
//        log.info("导入账号的地址" + realPath);
//        //转存文件
//        multipartFile.transferTo(new File(realPath));
//        autoRegisterService.importAccout(realPath, currentId);
//        Result<String> success = Result.success(null, "已经提交处理,请勿重复提交");
//        return success;
//    }


    /**
     * 用户提交接码平台的id过来
     *
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody EMaUserInfoDTO eMaUserInfoDTO) {
        EMaUerInfo eMaUerInfo = autoRegisterService.loginBySecret(eMaUserInfoDTO);
        if (eMaUerInfo == null) {
            Result<String> error = Result.error("账号或者密码错误");
            return error;
        }
        EMaUerInfoVO eMaUerInfoVO = new EMaUerInfoVO();
        BeanUtils.copyProperties(eMaUerInfo, eMaUerInfoVO);
        Result<EMaUerInfoVO> success = Result.success(eMaUerInfoVO, "登录成功！");
        return success;
    }

    /**
     * 用户提交接码平台的id过来
     *
     * @return
     */
    @PostMapping("/login2")
    public Result login2(@RequestBody EMaUserInfoDTO eMaUserInfoDTO) throws JsonProcessingException {
        EMaUerInfo eMaUerInfo = autoRegisterService.loginBySecret2(eMaUserInfoDTO);
        if (eMaUerInfo == null) {
            Result<String> error = Result.error("账号或者密码错误");
            return error;
        }
        EMaUerInfoVO eMaUerInfoVO = new EMaUerInfoVO();
        BeanUtils.copyProperties(eMaUerInfo, eMaUerInfoVO);
        Result<EMaUerInfoVO> success = Result.success(eMaUerInfoVO, "登录成功！");
        return success;
    }


    @PostMapping("/loginreq")
    public Result loginReq(@RequestBody EMaCheckDTO eMaCheckDTO) {
        String msg = autoRegisterService.loginReq(eMaCheckDTO);
        if (msg.contains("用户不存在") || msg.contains("密码错误")) {
            Result<String> error = Result.error(msg);
            return error;
        }
        List<AutoRegister> autoRegisters = autoRegisterService.getRegisterRecord(eMaCheckDTO.getUser_id());
        //查询用户所有的号
        Result<List<AutoRegister>> success = Result.success(autoRegisters, "查询成功！");
        return success;
    }


    @PostMapping("/register")
    public Result register(@RequestBody EMaRegisterDTO eMaRegisterDTO) {
        try {
            EMaCheckDTO eMaCheckDTO = new EMaCheckDTO();
            BeanUtils.copyProperties(eMaRegisterDTO, eMaCheckDTO);
            String realPath = httpServletRequest.getServletContext().getRealPath("/");
            String msg = autoRegisterService.loginReq(eMaCheckDTO);
            if (msg.contains("用户不存在") || msg.contains("密码错误")) {
                Result<String> error = Result.error(msg);
                return error;
            }
            autoRegisterService.register(eMaRegisterDTO, realPath);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        Result<String> success = Result.success(null, "已经提交处理请留意进度条");
        return success;
    }

    @PostMapping("/register2")
    public Result register2(@RequestBody EMaRegisterDTO eMaRegisterDTO) {
        try {
            EMaCheckDTO eMaCheckDTO = new EMaCheckDTO();
            BeanUtils.copyProperties(eMaRegisterDTO, eMaCheckDTO);
            String realPath = httpServletRequest.getServletContext().getRealPath("/");
            String msg = autoRegisterService.loginReq(eMaCheckDTO);
            if (msg.contains("用户不存在") || msg.contains("密码错误")) {
                Result<String> error = Result.error(msg);
                return error;
            }
            autoRegisterService.register2(eMaRegisterDTO, realPath);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        Result<String> success = Result.success(null, "已经提交处理请留意进度条");
        return success;
    }


    @GetMapping("/import")
    public Result importIdentityCard() throws IOException {
        autoRegisterService.importIdentityCard();
        Result<String> success = Result.success(null, "已经提交处理");
        return success;
    }

}
